//
//  BuyOrderViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/17.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "BuyOrderViewController.h"
#import "BuyOrderContentViewController.h"
#import "BargainViewController.h"
#import "GroupBookingViewController.h"

@interface BuyOrderViewController ()<UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *titleScrollView;
@property (strong, nonatomic) UIView *sliderView;
@property (strong, nonatomic) UIScrollView *contentScrollView;

@property (assign, nonatomic) CGFloat labelTotolWidth;

@property (strong, nonatomic) NSArray *arr;

@property (strong, nonatomic) NSMutableArray *labelArr;
@property (strong, nonatomic) NSMutableArray *labelxArr;


@end

@implementation BuyOrderViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = Colorf2f2f2;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.title = @"我的购买订单";
    
    self.labelArr = [NSMutableArray array];
    self.labelxArr = [NSMutableArray array];
    
    [self.view addSubview:self.titleScrollView];
    [self.view addSubview:self.contentScrollView];
    
    __weak typeof(self) weakSelf = self;
    [self.titleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.height.mas_equalTo(jisuan(86));
        
    }];
    [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.titleScrollView.mas_bottom);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
        
    }];
    
    [self.view layoutIfNeeded];
    
    NSLog(@"%@",NSStringFromCGRect(self.contentScrollView.frame));
    
    
    [self setupTitleScrollView];
    [self addChildVCs];
    
    // 添加默认控制器
    BargainViewController *firstVc = [self.childViewControllers firstObject];
    firstVc.view.frame = self.contentScrollView.bounds;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.contentScrollView addSubview:firstVc.view];
    // 设置内容的 size
    CGFloat contentW = self.childViewControllers.count * WIDTH;
    self.contentScrollView.contentSize = CGSizeMake(contentW, 0);
    
}

#pragma mark -label点击事件
- (void)labelGestureClick:(UIGestureRecognizer *)recognizer {
    
    NSLog(@"label点击事件");
    
    UILabel *label = (UILabel *)recognizer.view;
    
    CGFloat contentSetOffx = label.tag * WIDTH;
    
    [self.contentScrollView setContentOffset:CGPointMake(contentSetOffx, 0) animated:YES];

    
    for (int i = 0 ; i < self.childViewControllers.count; i ++) {
        
        UILabel *temp = self.titleScrollView.subviews[i] ;
        
        temp.font = Font(20);
        
    }
    
    label.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:jisuan(24)];
    
    CGFloat x = [self.labelxArr[label.tag] floatValue];
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        
        CGRect rect = weakSelf.sliderView.frame;
        rect.origin.x = x;
        rect.size.width = label.width;
        weakSelf.sliderView.frame = rect;
        
    }];
    
}

#pragma mark -delegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    NSInteger index = scrollView.contentOffset.x / WIDTH;
    
    BuyOrderContentViewController *vc = self.childViewControllers[index];
    UILabel *label = self.titleScrollView.subviews[index];
    
    for (int i = 0; i < self.labelArr.count;  i ++) {
        
        UILabel *temp = self.labelArr[i];
        temp.font = Font(20);
    }
    
    label.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:jisuan(24)];;
    
    CGFloat width = self.titleScrollView.frame.size.width;
    CGFloat offsetX = label.center.x - width * 0.5;
    // 最大偏移量
    CGFloat maxOffsetx = self.titleScrollView.contentSize.width - width;
    if (offsetX < 0) {
        offsetX = 0;
    } else if (offsetX > maxOffsetx) {
        offsetX = maxOffsetx;
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:NO];
    
    // 如果子控制器的view已经在上面,就直接返回
    if (vc.view.superview) {
        [vc.view removeFromSuperview];
    }
    
    // 添加
    CGFloat vcW = scrollView.frame.size.width;
    CGFloat vcH = scrollView.frame.size.height;
    CGFloat vcY = 0;
    CGFloat vcX = index * vcW;
    vc.view.frame = CGRectMake(vcX, vcY, vcW, vcH);
    [scrollView addSubview:vc.view];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    
    CGFloat scale = scrollView.contentOffset.x / WIDTH;
    
    NSInteger index = (int)scale;
    
//    [self isHidden:scrollView.contentOffset.x];
    
    UILabel *temp = self.titleScrollView.subviews[index];
    
    CGFloat x = (temp.width + jisuan(78) + jisuan(16)) * scale + jisuan(80);
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3
                     animations:^{
                         
                         CGRect rect = weakSelf.sliderView.frame;
                         rect.origin.x = x;
                         rect.size.width = temp.width;
                         weakSelf.sliderView.frame = rect;
                         
                     }];
    
}

/**
 *  当scrollview停止滚动时调用这个方法 (用户手动触发的动画结束,会调用这个方法)
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}


#pragma mark -分类标题scrollview布局
- (void)setupTitleScrollView {
    
    CGFloat leftMargin = jisuan(80);
    
    CGFloat labelMargin = jisuan(78);
    
    self.labelTotolWidth = leftMargin;
    
    for (int i = 0; i < self.arr.count; i ++) {
        
        UILabel *label = [[UILabel alloc] init];
        label.tag = i;
        label.userInteractionEnabled = YES;
        label.text = self.arr[i];
        label.font = Font(20);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = Color333333;
        [label sizeToFit];
        
        [self.labelArr addObject:label];
        
        if (i == 0) {
            
        }else {
            
            UILabel *tempL = self.labelArr[i - 1];
            CGFloat width = tempL.size.width + jisuan(16);
            self.labelTotolWidth = self.labelTotolWidth + width+ labelMargin;
            
        }
        
        [self.labelxArr addObject:[NSString stringWithFormat:@"%.2f",self.labelTotolWidth]];
        
        label.frame = CGRectMake(self.labelTotolWidth, 0, label.size.width + jisuan(16), jisuan(100));
        [self.titleScrollView insertSubview:label atIndex:i];
        
        UITapGestureRecognizer *labelGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelGestureClick:)];
        [label addGestureRecognizer:labelGesture];
        
    }
    
    UILabel *firstL = self.labelArr.firstObject;
    firstL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:jisuan(24)];
    
    UILabel *temp = [self.labelArr lastObject];
    CGFloat scrollviewWith = self.labelTotolWidth + leftMargin + temp.width;
    
    CGSize size = CGSizeMake(scrollviewWith, 0);
    self.titleScrollView.contentSize = size;
    
    [self.titleScrollView addSubview:self.sliderView];
    UILabel *label = [self.titleScrollView.subviews firstObject];
    self.sliderView.frame = CGRectMake([self.labelxArr.firstObject floatValue], jisuan(78), label.width, jisuan(6));
    
}

#pragma mark -添加自控制器
- (void)addChildVCs {
    BuyOrderContentViewController *vc1 = [[BuyOrderContentViewController alloc] init];
    vc1.title = @"待支付";
    vc1.tradeType = @"1";
    [self addChildViewController:vc1];
    
    
    GroupBookingViewController *vc6 = [[GroupBookingViewController alloc] init];
    vc6.title = @"拼团中";
    vc6.tradeType = @"2";
    [self addChildViewController:vc6];
    
    BargainViewController *vc5 = [[BargainViewController alloc] init];
    vc5.title = @"砍价中";
    vc5.tradeType = @"1";
    [self addChildViewController:vc5];
    
    BuyOrderContentViewController *vc7 = [[BuyOrderContentViewController alloc]init];
    vc7.title = @"待审核";
    vc7.tradeType = @"5";
    [self addChildViewController:vc7];
    
    
    BuyOrderContentViewController *vc2 = [[BuyOrderContentViewController alloc] init];
//    vc2.title = @"已支付";
    vc2.title = @"待发货";
    vc2.tradeType = @"2";
    [self addChildViewController:vc2];
    
    BuyOrderContentViewController *vc3 = [[BuyOrderContentViewController alloc] init];
//    vc3.title = @"已发货";
    vc3.title = @"待收货";
    vc3.tradeType = @"3";
    [self addChildViewController:vc3];
    
    
    BuyOrderContentViewController *vc4 = [[BuyOrderContentViewController alloc] init];
    //vc4.title = @"已收货";
    vc4.title = @"已完成";
    vc4.tradeType = @"4";
    [self addChildViewController:vc4];
    
    
//    for (int i = 0; i < self.labelArr.count; i ++) {
//
//        BuyOrderContentViewController *contentVC = [[BuyOrderContentViewController alloc] init];
//
//        contentVC.tradeType = i;
//
//        [self addChildViewController:contentVC];
//    }
//
//    BuyOrderContentViewController *vc = self.childViewControllers.firstObject;
//
//    self.automaticallyAdjustsScrollViewInsets = NO;
//    vc.view.frame = self.contentScrollView.bounds;
//    [self.contentScrollView addSubview:vc.view];
//
//    self.contentScrollView.contentSize = CGSizeMake(WIDTH * self.childViewControllers.count, 0);
    
}

#pragma mark -lazy
- (UIScrollView *)titleScrollView {
    
    if (!_titleScrollView) {
        
        _titleScrollView = [[UIScrollView alloc] init];
        _titleScrollView.backgroundColor = WhiteColor;
        
        _titleScrollView.showsVerticalScrollIndicator = NO;
        _titleScrollView.showsHorizontalScrollIndicator = NO;
        
        if (@available(iOS 11.0, *)) {
            
            _titleScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        
    }
    
    return _titleScrollView;
}

- (UIView *)sliderView {
    
    if (!_sliderView) {
        
        _sliderView = [[UIView alloc] init];
        _sliderView.backgroundColor = Colorff0000;
        
    }
    
    return _sliderView;
}

- (UIScrollView *)contentScrollView {
    
    if (!_contentScrollView) {
        
        _contentScrollView = [[UIScrollView alloc] init];
        _contentScrollView.backgroundColor = Colorf2f2f2;
        _contentScrollView.delegate = self;
        _contentScrollView.bounces = NO;
        _contentScrollView.pagingEnabled = YES;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.showsVerticalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _contentScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _contentScrollView;
}

- (NSArray *)arr {
    
    if (!_arr) {
        
//        _arr = @[ @"砍价中", @"拼团中", @"待支付", @"已支付", @"已发货", @"已收货"];
        _arr = @[ @"待支付", @"拼团中", @"砍价中", @"待审核" , @"待发货", @"待收货", @"已完成"];
        
    }
    
    return _arr;
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
